package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.dao.UecPartyLogDao;
import cn.ffcs.uec.staff.model.UecPartyLog;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecPartyLogDao")
@Transactional
public class UecPartyLogDaoImpl extends BaseDaoImpl implements UecPartyLogDao {

	@Override
	public PageInfo queryPageInfoByUecPartyLog(UecPartyLog uecPartyLog, int current,
			int pageSize) {
		if (null != uecPartyLog) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_PARTYLOG T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecPartyLog.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecPartyLog.getPkPsndoc())) {//判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPartyLog.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecPartyLog.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPartyLog.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecPartyLog.class);
		}
		return null;
	}

	@Override
	public UecPartyLog findUecPartyLogById(UecPartyLog uecPartyLog) {
		if (null != uecPartyLog) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_PARTYLOG T WHERE 1=1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecPartyLog.getPartylogId())) {
				sb.append(" AND T.PARTYLOG_ID = ? ");
				params.add(uecPartyLog.getPartylogId());
			}
			List<UecPartyLog> partyLogs = this.jdbcFindList(sb.toString(), params, UecPartyLog.class);
			if (null != partyLogs && partyLogs.size() > 0) {
				return partyLogs.get(0);
			}
		}
		return uecPartyLog;
	}

	@Override
	public PageInfo queryPageInfoByUecPartyLogOnApproval(
			UecPartyLog uecPartyLog, int current, int pageSize) {
		if (null != uecPartyLog) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_PARTYLOG T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecPartyLog.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecPartyLog.getPkPsndoc())) {//判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPartyLog.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecPartyLog.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPartyLog.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecPartyLog.class);
		}
		return null;
	}

	@Override
	public UecPartyLog getUecPartyLogByUecOutStaffInfo(
			UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_PARTYLOG T WHERE T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ?");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			List<UecPartyLog> list = this.jdbcFindList(sb.toString(), params,
					UecPartyLog.class);
			if (null != list && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

}
